package com.kharthick.game;

import java.awt.*;
import java.util.List;

import com.kharthick.pwGame.sprites.Sprite;
import com.kharthick.pwGame.sprites.SpriteFactory;

/*
 * Generate the menu for the game
 */

public class Menu {
	
	MapNameFilter filter;
	Graphics2D buffer;
	Core applet;
	Sprite splash;
	Sprite title;
	Font font;
	Marker bullet;
	
	//List<String> mapNames;
	String[] mapNames;
	
	private int maxPlayers;
	private int numberOfPlayers;
	private int mapChoice;
	private int numberOfChoices;
	
	public Menu(Graphics2D buf, MediaTracker tracker, Core core){
		buffer = buf;
		applet = core;
		
		//this doesn't work
		applet.setBackground(Color.black);
		//oldNumOfPlay = 1;
		numberOfPlayers = 1;
		mapChoice = 0;
		
		font = new Font("Arial", Font.BOLD, 16);
		buffer.setFont(font);
		buffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		title = SpriteFactory.getSprite("title.jpg", tracker);
		splash = SpriteFactory.getSprite("aknight.gif", tracker);
	}
	
	public void initial() {
		applet.showStatus("game menu");
		
		filter = new MapNameFilter();
		mapNames = filter.getMapNames("D:\\source_allen\\pwgui\\dou");
		numberOfChoices = mapNames.length;
		
		int startX = 170;
		int startY = 270;
		int positions = 3;
		int increment = 20;
		boolean vertical = true;
		
		bullet = new Marker(startX, startY, positions,
				increment, vertical, buffer);
		
		draw();
	}
	
	public void draw(){
		buffer.drawImage(title.getImage(), 5, 80, null);
		buffer.drawImage(splash.getImage(), 180, 130, null);
		
		buffer.setColor(Color.red);
		bullet.draw();
		
		buffer.drawString("Number of Players:", 190, 280);
		buffer.drawString(String.valueOf(numberOfPlayers), 340, 280);
		buffer.drawString("Map: ", 190, 300);
		
		buffer.drawString(mapNames[mapChoice], 240, 300);
		buffer.drawString("Help", 190, 320);
		
		buffer.drawString("Press SPACE to start",190,340);
	}
	
	public String getMapName(){
		return mapNames[mapChoice];
	}
	
	public int getNumberOfPlayers(){
		return numberOfPlayers;
	}
	
	/**
	 VK_LEFT = 37, VK_UP = 38
	 VK_RIGHT = 39, VK_DOWN = 40
	 
	 		   38
			37 40 39
	     
	      VK_ENTER = 10
	*/
	public void menuWait(int pressedKey){
		
		bullet.move(pressedKey);
		int pos = bullet.geCurrentPosition();

		if(pressedKey == 39 || pressedKey == 37){
			if(pos == 1)
				choosePlay(pressedKey);
			else if(pos == 2)
				chooseMap(pressedKey);
			else if(pos == 3);
		}
		
		draw();
	}
	
	private void choosePlay(int pressedKey){
		if(pressedKey == 39){
			numberOfPlayers++;
			if(numberOfPlayers > 4)
				numberOfPlayers = 1;
		}
		else if(pressedKey == 37){
			numberOfPlayers--;
			if(numberOfPlayers < 1)
				numberOfPlayers = 4;
		}

		if(numberOfPlayers == 1)
			mapChoice = 0;
		else if(mapChoice == 0)
			mapChoice = 1;
	}
	
	private void chooseMap(int pressedKey){
		if(numberOfPlayers == 1){
			mapChoice = 0;
		}
		else if(pressedKey == 39){
			mapChoice++;
			if(mapChoice == numberOfChoices)
				mapChoice = 1;
		}
		else if(pressedKey == 37){
			mapChoice--;
			if(mapChoice < 1)
				mapChoice = numberOfChoices - 1;
		}
	}
	
}
